package com.qidianit.mes.pro.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.mes.pojo.pro.ProductionOrder;
import com.qidianit.mes.pro.mapper.ProductionOrderMapper;
import com.qidianit.mes.pro.service.ProductionOrderService;
import com.qidianit.mes.pro.vo.ProductionOrderMaterialWithUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author: shanzhe
 * @Date: 2025/9/20 - 14:31
 */
@Service
public class ProductionOrderServiceImpl implements ProductionOrderService {
    @Autowired
    private ProductionOrderMapper productionOrderMapper;

    @Override
    public PageInfo<ProductionOrderMaterialWithUnit> queryProductionOrderListWithMultiConditionsAndPage(Integer pageNum, Integer pageSize, String productionOrderCode, String productionOrderName, Integer productionOrderSource, String materialCode, String materialName, String customerCode, Integer productionOrderType, String requestDate,Integer status) {
       PageHelper.startPage(pageNum, pageSize);
       List<ProductionOrderMaterialWithUnit> productionOrderMaterialWithUnits = productionOrderMapper.queryProductionOrderListWithMultiConditionsAndPage(pageNum,pageSize,productionOrderCode,productionOrderName,productionOrderSource,materialCode,materialName,customerCode,productionOrderType,requestDate,status);
       PageInfo<ProductionOrderMaterialWithUnit> pageInfo = new PageInfo<>(productionOrderMaterialWithUnits);
       return pageInfo;
    }

    @Override
    public int addProduction(ProductionOrder productionOrder) {
        return productionOrderMapper.addProduction(productionOrder);
    }

    @Override
    public ProductionOrder queryProductionOrderByCode(String productionOrderCode) {
        return productionOrderMapper.queryProductionOrderByCode(productionOrderCode);
    }

    @Override
    public int updateProductionOrder(ProductionOrder productionOrder) {
        ArrayList<ProductionOrder> productionOrderResult = productionOrderMapper.queryProductionOrderAll();
        for (ProductionOrder order : productionOrderResult) {
            if (productionOrder.getProductionOrderCode().equals(order.getProductionOrderCode())
            && !productionOrder.getId().equals(order.getId())) {
                return -1;
            }
        }
        return productionOrderMapper.updateProductionOrder(productionOrder);
    }

    @Override
    public int deleteProductionOrderById(Long id) {
        return productionOrderMapper.deleteProductionOrderById(id);
    }


}
